package cn.sciento.transfer.app.service.impl;

import com.alibaba.fastjson.JSON;
import cn.sciento.transfer.app.service.EventSendService;
import cn.sciento.transfer.app.service.MessageSendService;
import cn.sciento.transfer.domain.entity.EventSend;
import cn.sciento.transfer.domain.repository.EventSendRepository;
import cn.sciento.transfer.infra.rocketmq.vo.MqMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EventSendServiceImpl implements EventSendService {
  private static final Logger logger = LoggerFactory.getLogger(EventSendServiceImpl.class);
  
  @Autowired
  private EventSendRepository eventSendRepository;
  
  @Autowired
  private MessageSendService messageSendService;
  
  public EventSend insertEventSend(EventSend eventSend) {
    try {
      eventSend.setProcessStatus("P");
      MqMessage mqMessage = JSON.parseObject(eventSend.getMessages(), MqMessage.class);
      eventSend.setProduceTime(mqMessage.getProduceTime());
      this.eventSendRepository.insertSelective(eventSend);
    } catch (Exception e) {
      logger.error(" insert EventSend error", e);
      return null;
    } 
    this.messageSendService.addMsgToMQ(eventSend);
    return eventSend;
  }
}
